package yns.springboot.guide.demo.controller.data.db;

import java.time.LocalDateTime;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import tools.jackson.databind.ObjectMapper;
import yns.springboot.guide.demo.controller.BaseController;
import yns.springboot.guide.demo.entity.UserEntity;
import yns.springboot.guide.demo.service.mybatis.UserServiceAnnotationByMybatis;
import yns.springboot.guide.demo.service.mybatis.UserServiceXMLByMybatis;

@RestController()
@RequestMapping("/db/mybatis")
public class TestMybatisController extends BaseController {
	
	// http://localhost:8080/db/mybatis/mapperAnnotation/select
	// http://localhost:8080/db/mybatis/mapperAnnotation/add
	// http://localhost:8080/db/mybatis/mapperAnnotation/delete
	// http://localhost:8080/db/mybatis/mapperAnnotation/update
	
	// http://localhost:8080/db/mybatis/mapperXML/select
	// http://localhost:8080/db/mybatis/mapperXML/add
	// http://localhost:8080/db/mybatis/mapperXML/delete
	// http://localhost:8080/db/mybatis/mapperXML/update
	
	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	 
	@Autowired
	private UserServiceAnnotationByMybatis userServiceAnnotation;
	
	@Autowired
	private UserServiceXMLByMybatis userServiceXML;
	
	@RequestMapping("/mapperAnnotation/select")
	public String testAnnotationSelect() {
		logger.info(httpServletRequest.getRequestURI());
		
		List<UserEntity> userEntities = userServiceAnnotation.getUser();
		
		ObjectMapper objectMapper = new ObjectMapper();
		String result = objectMapper.writeValueAsString(userEntities);
		
//		String result = "";
//		try {
//			result = objectMapper.writeValueAsString(userEntities);
//		} catch (JsonProcessingException e) {
//			e.printStackTrace();
//		}
		
		return result;
	}
	
	@RequestMapping("/mapperAnnotation/add")
	public String testAnnotationAdd() {
		logger.info(httpServletRequest.getRequestURI());
		
		UserEntity userEntity = new UserEntity();
		userEntity.setName("test");
		userEntity.setAge(34);
		userEntity.setSex(1);
		userEntity.setDescribe("master Annotation "  + LocalDateTime.now());
		
		userServiceAnnotation.addUser(userEntity);
		
		String result = "add";
		
		return result ;
	}
	
	@RequestMapping("/mapperAnnotation/delete")
	public String testAnnotationDelete() {
		logger.info(httpServletRequest.getRequestURI());
		
		userServiceAnnotation.deleteUser(0);
		
		String result = "delete";
		
		return result ;
	}
	
	@RequestMapping("/mapperAnnotation/update")
	public String testAnnotationUpdate() {
		logger.info(httpServletRequest.getRequestURI());
		
		UserEntity userEntity = userServiceAnnotation.getUser().get(0);
		userEntity.setDescribe("master Annotation update : " + LocalDateTime.now());
		
		userServiceAnnotation.updateUser(userEntity);
		
		String result = "update id :" + userEntity.getId();
		
		return result ;
	}
	
	
	@RequestMapping("/mapperXML/select")
	public String testXMLSelect() {
		logger.info(httpServletRequest.getRequestURI());
		
		List<UserEntity> userEntities = userServiceXML.getUser();
		
		ObjectMapper objectMapper = new ObjectMapper();
		String result = objectMapper.writeValueAsString(userEntities);
		
//		String result = "";
//		try {
//			result = objectMapper.writeValueAsString(userEntities);
//		} catch (JsonProcessingException e) {
//			e.printStackTrace();
//		}
		
		return result;
	}
	
	
	@RequestMapping("/mapperXML/add")
	public String testXMLAdd() {
		logger.info(httpServletRequest.getRequestURI());
		
		UserEntity userEntity = new UserEntity();
		userEntity.setName("test");
		userEntity.setAge(34);
		userEntity.setSex(1);
		userEntity.setDescribe("master XML "  + LocalDateTime.now());
		
		userServiceXML.addUser(userEntity);
		
		String result = "add";
		
		return result ;
	}
	
	@RequestMapping("/mapperXML/delete")
	public String testXMLDelete() {
		logger.info(httpServletRequest.getRequestURI());
		
		userServiceXML.deleteUser(0);
		
		String result = "delete";
		
		return result ;
	}
	
	@RequestMapping("/mapperXML/update")
	public String testXMLUpdate() {
		logger.info(httpServletRequest.getRequestURI());
		
		UserEntity userEntity = userServiceXML.getUser().get(0);
		userEntity.setDescribe("master XML update : " + LocalDateTime.now());
		
		userServiceXML.updateUser(userEntity);
		
		String result = "update id :" + userEntity.getId();
		
		return result ;
	}

}
